package http

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

/*********
发送http请求
*/

func DoPost(url string, req string, contentType string) ([]byte, error) {

	if contentType == "" {
		contentType = "application/json"
	}

	response, err := post(url, contentType,
		req)

	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	return response, err
}

func post(url string, contenType string, payload string) ([]byte, error) {

	resp, err := http.Post(url, contenType, strings.NewReader(payload))

	if err != nil {
		fmt.Println("get error", err)
		return nil, err
	}
	//body ,err:= ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	//if err != nil{
	//    fmt.Println("read body",err)
	//    return
	//}
	body, err := ioutil.ReadAll(resp.Body)

	return body, err
}
